package com.ruoyi.system.controller;


import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.threadlocal.UserThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WorkOrder;
import com.ruoyi.system.service.IWorkOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工单管理Controller
 * 
 * @author ruoyi
 * @date 2023-06-02
 */
@RestController
@RequestMapping("/ruoyi-workorder/order")
public class WorkOrderController extends BaseController
{
    @Autowired
    private IWorkOrderService workOrderService;

    /**
     * 查询工单管理列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-workorder:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrder workOrder)
    {
        startPage();
        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        return getDataTable(list);
    }

    /**
     * 导出工单管理列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-workorder:order:export')")
    @Log(title = "工单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkOrder workOrder)
    {
        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        ExcelUtil<WorkOrder> util = new ExcelUtil<WorkOrder>(WorkOrder.class);
        util.exportExcel(response, list, "工单管理数据");
    }

    /**
     * 获取工单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-workorder:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(workOrderService.selectWorkOrderById(id));
    }

    /**
     * 新增工单管理
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-workorder:order:add')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkOrder workOrder)
    {
        //创建一个WorkOrder对象
        WorkOrder workOrders = new WorkOrder();
        //把传过来的数据复制粘贴到workOrders中
        BeanUtils.copyProperties(workOrder,workOrders);
        //添加工单编码   UUID生成工单编码
        workOrders.setOrderCode(get8UUID());
        //添加创建时间
        workOrders.setCreatedAt(new Date());
        return toAjax(workOrderService.insertWorkOrder(workOrders));
    }

    /**
     * 修改工单管理
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-workorder:order:edit')")
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkOrder workOrder)
    {
        //创建一个WorkOrder对象
        WorkOrder workOrders = new WorkOrder();
        //把传过来的数据复制粘贴到workOrders中
        BeanUtils.copyProperties(workOrder,workOrders);
        //添加修改时间
        workOrders.setUpdatedAt(new Date());
        return toAjax(workOrderService.updateWorkOrder(workOrder));
    }

    /**
     * 删除工单管理
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-workorder:order:remove')")
    @Log(title = "工单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(workOrderService.deleteWorkOrderByIds(ids));
    }

    /**
     * 派单处理
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi-workorder:order:send')")
//    @Log(title = "工单管理", businessType = BusinessType.DELETE)
    @Anonymous
    @GetMapping("/send/{id}/{name}")
    public void send(@PathVariable("id") Long id,@PathVariable("name")String name){

        workOrderService.updateWorkOrderById(id,name);

    }
    /**
     * 回复处理
     */
//    @PreAuthorize("@ss.hasPermi('ruoyi-workorder:order:huifu')")
//    @Log(title = "工单管理", businessType = BusinessType.DELETE)
    @Anonymous
    @GetMapping("/huifu/{id}/{name}")
    public void huifu(@PathVariable("id") Long id,@PathVariable("name")String name){

        workOrderService.updateWorkOrderByIds(id,name);

    }

    /**
     *
     */
    @Anonymous
    @GetMapping("/getUserName")
    public AjaxResult lists(){

        return AjaxResult.success(UserThreadLocalUtils.getUserName());

    }


    /**
     * 获得8个长度的十六进制的UUID
     * @return UUID
     */
    public static String get8UUID() {
        UUID id = UUID.randomUUID();
        String[] idd = id.toString().split("-");
        return idd[0];
    }
}
